package com.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import com.items.person;

import java.util.ArrayList;
import java.util.List;

public class DBoperate {
    private DBHelp dbhelp;
    private SQLiteDatabase db;//数据库操作对象
    public DBoperate(DBHelp dbhelp) {
        this.dbhelp = dbhelp;
        try{
            db=this.dbhelp.getWritableDatabase();
        }catch (SQLiteException ex){
            db=this.dbhelp.getReadableDatabase();
        }
    }

    public List<person> getAll(){
        //获取所有数据
        String[] columns = {"id","name","phone","phone_2"};
        Cursor cursor = db.query("a", columns, null, null, null, null, null);
        List<person> ans=new ArrayList<person>();
        if (cursor.moveToFirst()){
            do{
                //获取数据索引
                int idIndex=cursor.getColumnIndex("id");
                int nameIndex=cursor.getColumnIndex("name");
                int phoneIndex=cursor.getColumnIndex("phone");
                int phone2Index=cursor.getColumnIndex("phone_2");
                //获取所有数据
                int id=cursor.getInt(idIndex);
                String name=cursor.getString(nameIndex);
                String phone_1=cursor.getString(phoneIndex);
                String phone_2=cursor.getString(phone2Index);
                //添加数据
                ans.add(new person(id,name,phone_1,phone_2));
            }while(cursor.moveToNext());
        }
        return ans;
    }

    public boolean addData(person p){
        //插入成功返回true，失败返回false
        ContentValues values = new ContentValues();
        values.put("name",p.getName());
        values.put("phone",p.getPhone_1());
        values.put("phone_2",p.getPhone_2());
        long rowId = db.insert("a", null, values);
        return rowId != -1;
    }

    public boolean deleteData(person pp){
        String[] d={pp.getName()};
        int rows=db.delete("a","name=?",d);
        return rows > 0;
    }

    public person search(String value,String type){
        String[] columns = {"id","name","phone","phone_2"};
        String selection;
        String[] selectionArgs = {value};
        if(type.equals("姓名")){
            selection="name=?";
        }
        else if(type.equals("主号码")){
            selection="phone=?";
        }
        else{
            selection="phone_2=?";
        }
        Cursor cursor = db.query("a", columns, selection, selectionArgs, null, null, null);
        if(cursor.moveToFirst()){
            //如果有数据
            int idIndex=cursor.getColumnIndex("id");
            int nameIndex=cursor.getColumnIndex("name");
            int phoneIndex=cursor.getColumnIndex("phone");
            int phone2Index=cursor.getColumnIndex("phone_2");
            //获取所有数据
            int id=cursor.getInt(idIndex);
            String name=cursor.getString(nameIndex);
            String phone_1=cursor.getString(phoneIndex);
            String phone_2=cursor.getString(phone2Index);
            return new person(id,name,phone_1,phone_2);
        }
        //没有查询到数据
        return null;
    }
}
